package com.senven.books.modules.system.log.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.senven.books.modules.system.log.entity.SysLog;
import com.senven.books.modules.system.log.entity.form.SysLogForm;
import com.senven.books.modules.system.log.service.ISysLogService;
import com.senven.books.modules.system.log.util.LogUtil;
import com.senven.books.utils.ResultEnum;
import com.senven.books.utils.ResultVO;
import com.senven.books.utils.ResultVOUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 * 系统日志 前端控制器
 * </p>
 *
 * @author longshengyi
 * @since 2021-08-13
 */
@RestController
@RequestMapping("/log/sys-log")
@Api(tags = {"日志服务接口"})
public class SysLogController {

    @Autowired
    private LogUtil logUtil;

    @Autowired
    private ISysLogService sysLogService;

/*    @ApiOperation(value = "系统日志")
    @ApiImplicitParams({@ApiImplicitParam(name = "type", value = "日志类型", required = true, dataType = "string"),
            @ApiImplicitParam(name = "message", value = "内容", required = true, dataType = "string")})
    @PutMapping
    public ResultVO saveLog(String type, String message) {
        logUtil.log(type, message);
        return ResultVOUtil.success();
    }


    @ApiOperation(value = "系统日志")
    @ApiImplicitParams(@ApiImplicitParam(name = "insertLog", value = "新增日志form", required = true, dataType = "SysLogForm"))
    @PostMapping({"/add"})
    public ResultVO saveLogDetail(@RequestBody SysLogForm insertLog) {
        logUtil.log(insertLog.getCategory(), insertLog.getOperationType(), insertLog.getContent());
        return ResultVOUtil.success();
    }*/



    @GetMapping({"/"})
    @ApiOperation("日志搜索")
    public ResultVO search(String category, String operationType, String content, String userName, @ApiParam(value = "当前页", name = "current", required = true, defaultValue = "1") @RequestParam(required = false) Integer current,
                           @ApiParam(value = "每页条数", name = "pageSize", required = true, defaultValue = "10") @RequestParam(required = false) Integer pageSize) {
        SysLog log = new SysLog();
        log.setStatus(null);
        log.setCategory(category);
        log.setContent(content);
        log.setUserName(userName);

        Page<SysLog> page = new Page<>(current, pageSize);
        return ResultVOUtil.success(ResultEnum.SUCCESS.getCode(), "查询成功", sysLogService.listLogPage(page, log));
    }

}
